package com.reizx.analysis;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import com.reizx.analysis.service.AnForegroundService;
import com.vivo.security.jni.Base64Util;
import com.vivo.security.jni.SecurityCryptor;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    public final String TAG = "analysis";
    public String encodeString = "ADJD6RWCxaKzOX_DU5ksE2hY6r_Sx5kzOe-CTek0W9Z0T9E9WNQsEXJuU-Ww6ykzOzEfEX4";
    //public String encodeString = "ADJD6RWCxaKzOX_DU5hg";
    public String decodeString = "{\"result\":tru123456789876543212345678987654321234567";
    //"ADJD6RWCxaKzOX_DU5ksE2hY6r_Sx5kzOe"  这个参数解析有问题
    @BindView(R.id.et_app_main)
    EditText mEtMain;

    Intent mAnForgroundIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mAnForgroundIntent = new Intent(this, AnForegroundService.class);
    }

    @OnClick(R.id.btn_app_base64_decode)
    public void decodeTest() {
        byte[] decodeBytes = SecurityCryptor.nativeBase64Decrypt(encodeString.getBytes());
        String decodeString = new String(decodeBytes);
        mEtMain.setText("the src encode String : \n" + encodeString);
        mEtMain.append("\n\n===================================\n\n");
        mEtMain.append("the decode String : \n" + decodeString);
        mEtMain.append("\n\n===================================\n\n");
        mEtMain.append("the Base64Util decode String : \n" + Base64Util.decode(encodeString));
        mEtMain.append("\n\n===================================\n\n");
        mEtMain.append("the Base64Util encode String : \n" + Base64Util.encode(decodeString));
    }

    //region service test
    @OnClick(R.id.btn_app_start_service)
    public void startService(){
        Log.d(TAG, "start service");
        startService(mAnForgroundIntent);
    }

    @OnClick(R.id.btn_app_stop_service)
    public void stopService(){
        stopService(mAnForgroundIntent);
    }

    @OnClick(R.id.btn_app_call_service)
    public void callService(){

    }
    //endregion


    //region base64 test
    @OnClick(R.id.btn_app_base64_encode)
    public void encodeTest() {

        mEtMain.setText("the src String : \n" + decodeString);
        mEtMain.append("\n\n===================================\n\n");

        String base64Encode = Base64Util.encode(decodeString);
        mEtMain.append("the Base64Util encode String : \n" + base64Encode);
        mEtMain.append("\n\n===================================\n\n");
        String base64Decode = Base64Util.decode(base64Encode);
        mEtMain.append("the Base64Util decode String : \n" + base64Decode);
    }

    @OnClick(R.id.btn_app_package_version)
    public void getPackageVersion(){
        try {
            PackageInfo pi = App.getApp().getPackageManager().getPackageInfo("com.bbk.appstore", 0);
            Log.d("AnalysisUnit", "the version name " + pi.versionName);
            Log.d("AnalysisUnit", "the version code " + pi.versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    //endregion
}
